package com.wh.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wh.basic.vo.AjaxResult;
import com.wh.system.annotation.PreAuthorize;
import com.wh.system.dto.RoleDto;
import com.wh.system.pojo.Role;
import com.wh.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.rmi.server.RemoteServer;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;


    @PreAuthorize(sn = "role/addOrUpd",name = "添加或修改角色")
    @PutMapping
    public AjaxResult addOrUpd(@RequestBody Role role) {
        if (role.getId() != null) {
            roleService.upd(role);
        } else {
            roleService.add(role);
        }
        return new AjaxResult();
    }
    @PreAuthorize(sn = "role/findByPage",name = "分页查找角色列表")
    @PostMapping
    public IPage<Role> findByPage(@RequestBody RoleDto roleDto){
        return roleService.findByPage(roleDto);
    }


    @PreAuthorize(sn = "role/findOne",name = "根据id查找角色")
    @GetMapping("/id")
    public Role findOne(@PathVariable("id") Long id){
        return roleService.findOne(id);
    }


    @PreAuthorize(sn = "role/patchDel",name = "批量删除角色")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        roleService.patchDel(ids);
        return new  AjaxResult();
    }


    @PreAuthorize(sn = "role/del",name = "根据id删除角色")
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        roleService.del(id);
        return new AjaxResult();
    }


    @PreAuthorize(sn = "role/findAll",name = "查询全部角色")
    @GetMapping
    public List<Role> findAll(){
        return roleService.findAll();
    }

    @PutMapping("/updatemenu")
    public AjaxResult updatemenu(@RequestBody Role role){
        return roleService.updatemenu(role);
    }

}
